use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::OnceLock;
use chrono::{DateTime, Local};
use crate::util::time::{format_time_by_format};

static  LOCAL_SEQ: OnceLock<DateTime<Local>> = OnceLock::new();
static COUNTER: AtomicU32 = AtomicU32::new(0);

pub fn init_local_seq(date: DateTime<Local>) {
    LOCAL_SEQ.get_or_init(|| date);
}

pub fn local_seq(login_user_id: &str)-> String {
    unsafe {
        let local_seq_data = LOCAL_SEQ.get();
        match local_seq_data {
            None => {
                format!("{}-{}-{}", format_time_by_format(&Local::now(), "%Y%m%d%H%M%S"), login_user_id, "00001")
            }
            Some(value) => {
                let seq = COUNTER.fetch_add(1, Ordering::SeqCst);
                format!("{}-{}-{}", format_time_by_format(&value, "%Y%m%d%H%M%S"), login_user_id, seq)
            }
        }
    }
}

struct LocalSeqData {
    server_date: Option<DateTime<Local>>,
    seq: usize,
}